PagesScripts.bookmarks = {};
PagesScripts.bookmarks.postItems = {};
PagesScripts.bookmarks.forumItems = {};
PagesScripts.bookmarks.bookmarkForDeletion = {};

PagesScripts.bookmarks.loadBookmarks = function() {
	$("#bookmark_list", $.mobile.activePage).html("");
	$("#forums_list", $.mobile.activePage).html("");
	PagesScripts.bookmarks.postItems = Config.getParam("bookmarks", "json");
	if (PagesScripts.bookmarks.postItems != null) {
		PagesScripts.bookmarks.postItems = eval(PagesScripts.bookmarks.postItems);
		for (var i=0;i<PagesScripts.bookmarks.postItems.length;i++) {
			$("#bookmarkItem").tmpl(PagesScripts.bookmarks.postItems[i]).appendTo("#bookmark_list", $.mobile.activePage);
		}
		$("#bookmark_list", $.mobile.activePage).listview("refresh");
	}
	
	PagesScripts.bookmarks.forumItems = Config.getParam("forumBookmarks", "json");
	if (PagesScripts.bookmarks.forumItems != null) {
		PagesScripts.bookmarks.forumItems = eval(PagesScripts.bookmarks.forumItems);
		for (var i=0;i<PagesScripts.bookmarks.forumItems.length;i++) {
			$("#forumBookmarkItem").tmpl(PagesScripts.bookmarks.forumItems[i]).appendTo("#forums_list", $.mobile.activePage);
		}
		$("#forums_list", $.mobile.activePage).listview("refresh");
	}
}

PagesScripts.bookmarks.SetDataFromBookmark = function(evt) {
	Config.CurrentForum = $.tmplItem(evt.target).data.CurrentForum;
	
	if ($.tmplItem(evt.target).data.CurrentPost != null) {
		Config.CurrentPost = $.tmplItem(evt.target).data.CurrentPost;
		PageManager.changePage("../post");
	}
	else {
		PageManager.changePage("../forum_posts");
	}
}

PagesScripts.bookmarks.addBookmark = function() {
	var bookmarksArray = Config.getParam("bookmarks", "json");
	if (bookmarksArray != null) {
		bookmarksArray = eval(bookmarksArray);
		for (var i=0;i<bookmarksArray.length;i++) {
			if (Config.GetCurrentForum().name == bookmarksArray[i].CurrentForum.name && Config.GetCurrentPost().id == bookmarksArray[i].CurrentPost.id) {
				return;
			}
		}
	}
	else {
		bookmarksArray = new Array();
	}
	bookmarksArray.push({CurrentForum:Config.GetCurrentForum(), CurrentPost:Config.GetCurrentPost()});
	Config.setParam("bookmarks", bookmarksArray, "json");
}

PagesScripts.bookmarks.addForumBookmark = function(evt) {
	var bookmarksArray = Config.getParam("forumBookmarks", "json");
	if (bookmarksArray != null) {
		bookmarksArray = eval(bookmarksArray);
		for (var i=0;i<bookmarksArray.length;i++) {
			if (evt != null) {
				if ($.tmplItem(evt.target).data.name == bookmarksArray[i].CurrentForum.name) {
					return;
				}
			}
			else {
				if (Config.GetCurrentForum().name == bookmarksArray[i].CurrentForum.name) {
					return;
				}
			}
		}
	}
	else {
		bookmarksArray = new Array();
	}
	if (evt != null)
		bookmarksArray.push({CurrentForum:$.tmplItem(evt.target).data});
	else
		bookmarksArray.push({CurrentForum:Config.GetCurrentForum()});
	Config.setParam("forumBookmarks", bookmarksArray, "json");
}

PagesScripts.bookmarks.setBookmarkForDeletion = function(evt) {
	PagesScripts.bookmarks.bookmarkForDeletion = $.tmplItem(evt.target).data;
}

PagesScripts.bookmarks.deleteBookmark = function() {
	var bookmarksArray = Config.getParam("bookmarks", "json");
	if (bookmarksArray != null) {
		bookmarksArray = eval(bookmarksArray);
		for (var i=bookmarksArray.length-1;i>-1;i--) {
			if (PagesScripts.bookmarks.bookmarkForDeletion.CurrentForum.name == bookmarksArray[i].CurrentForum.name && PagesScripts.bookmarks.bookmarkForDeletion.CurrentPost.id == bookmarksArray[i].CurrentPost.id) {
				bookmarksArray.splice(i, 1);
				break;
			}
		}
	}
	Config.setParam("bookmarks", bookmarksArray, "json");
	PagesScripts.bookmarks.loadBookmarks();
}

PagesScripts.bookmarks.setForumBookmarkForDeletion = function(evt) {
	PagesScripts.bookmarks.forumBookmarkForDeletion = $.tmplItem(evt.target).data;
}

PagesScripts.bookmarks.deleteForumBookmark = function() {
	var bookmarksArray = Config.getParam("forumBookmarks", "json");
	if (bookmarksArray != null) {
		bookmarksArray = eval(bookmarksArray);
		for (var i=bookmarksArray.length-1;i>-1;i--) {
			if (PagesScripts.bookmarks.forumBookmarkForDeletion.CurrentForum.name == bookmarksArray[i].CurrentForum.name) {
				bookmarksArray.splice(i, 1);
				break;
			}
		}
	}
	Config.setParam("forumBookmarks", bookmarksArray, "json");
	PagesScripts.bookmarks.loadBookmarks();
}